package com.dc.bobaoge.project.v2.to.v3.business.core;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dc.bobaoge.base.entity.message.common.po.MessageCount;
import com.dc.bobaoge.base.mapper.message.MessageCountMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreMessageBrowserCountMapper;
import com.dc.bobaoge.project.base.core.util.MessageCommonUtil;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * MessageBrowserCountResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-09 10:40:32
 */
@Slf4j
@Component
public class MessageBrowserCountResolveMethod implements ResolveMethod {

    @Resource
    private OldCoreMessageBrowserCountMapper oldCoreMessageBrowserCountMapper;
    @Resource
    private MessageCountMapper messageCountMapper;

    @Resource
    private MessageCommonUtil messageCommonUtil;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        var id = trigger.getTargetId();
        // 查询数据id
        var oldMessageBrowserCount = oldCoreMessageBrowserCountMapper.selectById(id);
        if (oldMessageBrowserCount == null) {
            throw new RuntimeException("未查询到浏览的信息");
        }
        // 去查询消息的准确信息
        var model = messageCommonUtil.getSimple(oldMessageBrowserCount.getBrowMsgId());
        if (model == null) {
            return;
        }
        var wrapper = new LambdaUpdateWrapper<MessageCount>()
                .eq(MessageCount::getFkMessageId, oldMessageBrowserCount.getBrowMsgId())
                .eq(MessageCount::getType, model.getType())
                .eq(MessageCount::getQuarter, model.getQuarter())
                .set(MessageCount::getOldBrowserCount, oldMessageBrowserCount.getMsgBrowserCont());
        messageCountMapper.update(wrapper);
    }


    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "message_browser_count";
    }

}
